<template>
  <a-spin :spinning="spinning">
    <search
      ref="search"
      :list="searchList"
      @search="handleSearch"
      @reset="handleReset"
      :moreHandles="moreHandles"
    ></search>
  </a-spin>
</template>

<script>
/**
 *Parser 申请列表的搜索
 *@Author
 *@Version
 *@property
 *@event
 */
import search from './search.vue'
import { getDropdownList, getCityReviewPersonList } from '@/api/common'
import { getDeptDropList } from '@/api/communityReviewManagement'
import lodash from 'lodash'
const dropValueForLabel = (data, valueKey = 'DROPKEY', labelKey = 'DROPVALUE') => {
  return data.map((e) => {
    return {
      value: e[valueKey],
      label: e[labelKey],
    }
  })
}
export default {
  components: {
    search,
  },
  props: {
    // 不要那些搜索条件
    filterFields: {
      type: Array,
      default: () => [],
    },
    // 自定义更多操作
    moreHandles: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      deptList: [],
      spinning: false,
      searchList: [
        {
          label: '申请编号',
          fields: 'ApplyNO',
          type: 'input',
          props: {
            placeholder: '请输入申请编号',
          },
        },
        {
          label: '申请人姓名',
          fields: 'Proposer',
          type: 'input',
          props: {
            placeholder: '请输入申请人姓名',
          },
        },
        {
          label: '身份证号',
          fields: 'IDcard',
          type: 'input',
          props: {
            placeholder: '请输入身份证号',
          },
        },
        {
          label: '电话号码',
          fields: 'Phone',
          type: 'input',
          props: {
            placeholder: '请输入电话号码',
          },
        },
        {
          label: '市级审核人',
          fields: 'CityVfyAgent',
          type: 'select',
          props: {
            placeholder: '请选择市级审核人',
            options: [],
            'show-search': true,
            optionFilterProp: 'children',
          },
        },
        {
          label: '所属部门(区)',
          fields: 'RegionNo',
          type: 'select',
          props: {
            placeholder: '请选择所属部门(区)',
            'show-search': true,
            optionFilterProp: 'children',
          },
          methods: {
            change: this.handleRegionNoChange,
          },
        },
        {
          label: '所属部门(社)',
          fields: 'TownshipNo',
          type: 'select',
          props: {
            placeholder: '请选择所属部门(社)',
            options: [],
            'show-search': true,
            optionFilterProp: 'children',
          },
        },
        {
          label: '保障类别',
          fields: 'ApplyClass',
          type: 'select',
          props: {
            placeholder: '请选择保障类别',
          },
        },
        {
          label: '保障方式',
          fields: 'CityVfyAssign',
          type: 'select',
          props: {
            placeholder: '请选择保障方式',
            options: [
              { value: '租赁住房补贴', label: '租赁住房补贴' },
              { value: '实物安置', label: '实物安置' },
              { value: '实物配售', label: '实物配售' },
              { value: '无配租', label: '无配租' },
            ],
          },
        },
        {
          label: '户籍类别',
          fields: 'ApplyType',
          type: 'radio',
          props: {
            options: [
              { value: '本市', label: '本市' },
              { value: '非本市', label: '非本市' },
            ],
          },
        },
        {
          label: '申请年份',
          fields: 'ZnameYear',
          type: 'year',
          props: {
            placeholder: '请选择申请年份',
          },
        },
        {
          label: '上报状态',
          fields: 'SubmitToCity',
          type: 'select',
          props: {
            placeholder: '请选择上报状态',
            options: [
              { value: '0', label: '未上报' },
              { value: '1', label: '已上报' },
              { value: '2', label: '已接收' },
              { value: '-1', label: '已退回' },
            ],
          },
        },
        {
          label: '申请人所属类别',
          fields: 'GroupOfApplicants',
          type: 'select',
          props: {
            placeholder: '请选择申请人所属类别',
          },
        },
        {
          label: '接收日期',
          fields: 'ProcessDate',
          type: 'range',
          props: {
            valueFormat: 'YYYY-MM-DD',
          },
        },
        {
          label: '市级接收人',
          fields: 'ProcessUser',
          type: 'select',
          props: {
            placeholder: '请选择市级接收人',
            options: [],
            'show-search': true,
            optionFilterProp: 'children',
          },
        },
        {
          label: '审核日期',
          fields: 'CityVfyDate',
          type: 'range',
          props: {
            valueFormat: 'YYYY-MM-DD',
          },
        },
        {
          label: '调查审核状态',
          fields: 'BeviewResult',
          type: 'select',
          props: {
            placeholder: '请选择调查审核状态',
            options: [
              { value: '未审核', label: '未审核' },
              { value: '符合', label: '符合' },
              { value: '不符合', label: '不符合' },
            ],
          },
        },
        {
          label: '区级审核状态',
          fields: 'VerifyResult',
          type: 'select',
          props: {
            placeholder: '请选择区级审核状态',
            options: [
              { value: '未审核', label: '未审核' },
              { value: '符合', label: '符合' },
              { value: '不符合', label: '不符合' },
            ],
          },
        },
        {
          label: '市级审核状态',
          fields: 'CityVfyResult',
          type: 'select',
          props: {
            placeholder: '请选择市级审核状态',
            options: [
              { value: '未审核', label: '未审核' },
              { value: '符合', label: '符合' },
              { value: '不符合', label: '不符合' },
            ],
          },
        },
        {
          label: '审批日期',
          fields: 'CityRfyDate',
          type: 'range',
          props: {
            valueFormat: 'YYYY-MM-DD',
          },
        },
        {
          label: '市级审批状态',
          fields: 'CityRfyResult',
          type: 'select',
          props: {
            placeholder: '请选择市级审批状态',
            options: [
              { value: '未审批', label: '未审批' },
              { value: '符合', label: '符合' },
              { value: '不符合', label: '不符合' },
            ],
          },
        },
      ],
    }
  },
  created() {
    this.fetchData()
    if (!this.$isEmpty(this.filterFields)) {
      // 筛选不需要的搜索条件
      this.searchList = this.searchList.filter((e) => !this.filterFields.includes(e.fields))
    }
  },
  methods: {
    fetchData() {
      this.spinning = true
      const getGroups = getDropdownList({ PmKey: 'Groups' })
      const getFamilyType = getDropdownList({ PmKey: 'FamilyType' })
      const getDeprList = getDeptDropList({ userid: this.$store.getters.userid })
      const getReviewPersonList = getCityReviewPersonList({ Userid: this.$store.getters.userid })
      Promise.all([getGroups, getFamilyType, getDeprList, getReviewPersonList])
        .then((res) => {
          const findIndex = (key) => {
            return this.searchList.findIndex((e) => e.fields === key) || ''
          }
          const setOptions = (data, key) => {
            try {
              this.searchList[findIndex(key)].props.options = data
            } catch (error) {}
          }
          this.deptList = res[2].pop().children
          const map = new Map()
          res[3].forEach((e) => {
            if (!map.has(e.name) && !this.$isEmpty(e.name)) {
              map.set(e.name, e)
            }
          })
          const personOptions = [...map.values()]
          setOptions(dropValueForLabel(res[0], 'DROPKEY'), 'GroupOfApplicants')
          setOptions(dropValueForLabel(res[1], 'DROPVALUE'), 'ApplyClass')
          setOptions(dropValueForLabel(this.deptList, 'ID', 'name'), 'RegionNo')
          setOptions(dropValueForLabel(personOptions, 'name', 'name'), 'CityVfyAgent')
          setOptions(dropValueForLabel(personOptions, 'name', 'name'), 'ProcessUser')
        })
        .finally(() => {
          this.spinning = false
        })
    },
    handleRegionNoChange(val) {
      this.$refs.search.setFieldsValue({
        TownshipNo: undefined,
      })
      const TownshipNoIndex = this.searchList.findIndex((e) => e.fields === 'TownshipNo')
      let options = this.deptList.find((e) => e.ID === val)
      if (options) {
        options = dropValueForLabel(options.children, 'ID', 'name')
      }
      this.searchList[TownshipNoIndex].props.options = options
    },
    handleSearch(e) {
      const keyArr = ['ProcessDate', 'CityVfyDate', 'CityRfyDate']
      const data = lodash.omit(e, keyArr)
      keyArr.forEach((key) => {
        if (!this.$isEmpty(e[key])) {
          data[`s${key}`] = e[key][0]
          data[`e${key}`] = e[key][1]
        }
      })
      this.$emit(
        'search',
        lodash.omitBy(data, (item) => !item)
      )
    },
    handleReset() {
      this.$emit('reset', {})
    },
    // 用于向后追加数据
    handleAppendFields(data) {
      this.searchList.push(data)
    },
  },
}
</script>
<style scoped></style>
